<?php

/* 
 * 权限管理列表
 * @2015-12-24 wzl
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class AdminActionModel extends db_help {
    public $keyFields = 'action_id';//设置主键
    public function __construct() {
        parent::__construct();
        $this->table_name = $this->table('admin_action');
    }
    public static function load(){
        return new self();
    }
    /**
     * 獲取被標為菜單的代碼
     * @return array 二維
     */
    public function get_menu(){
        $where = array(
            'is_menu' => 1,
        );
        $col = $this->getCol('action_code', $where);
        $menu = array();
        foreach ($col as $code) {
            $menu[] = "'{$code}'";
        }
        return $menu;
    }

    /**
     * 获取用户菜单
     * @param string $allot_code_str 结构必须是fields, fields1...
     * @return array 二维结果
     */
    public function get_user_menu($allot_code_str) {
        $sql = "select * from {$this->table_name} where is_menu = 1 and action_code in ({$allot_code_str})";        
        $result = $this->result_array($sql);
        return $result;
    }

    /**
     * 判断权限代码是否重复.
     * @param type $action_code
     * @return type
     */
    public function checkCode($action_code) {
        $where = array(
            'action_code' =>$action_code,
        );
        return $this->getRow('action_id', $where);
    }

    /**
     * 获取详情
     * @param type $action_id
     * @return type
     */
    public function get_info($action_id) {
        $where = array(
            'action_id' => $action_id,
        );
        return $this->getRow(null, $where);
    }
    /**
     * 新建
     * @param type $data
     * @return type
     */
    public function create_action($data) {
        $bool = $this->insert($data);
        return $bool;
    }
    /**
     * 编辑更新
     * @param type $action_id
     * @param type $data
     * @return type
     */
    public function edit($action_id, $data) {
        $where = array(
            'action_id' => $action_id,            
        );
        return $this->update($data, $where);
    }

    /**
     * 获取父ID和对应的名称
     * @return array 二维
     */
    public function get_parent(){        
        return $this->getAll('action_id, action_chinese');
    }
    
    /**
     * 获取关联的权限
     * @param type $action_code_str
     * @return type
     */
    public function get_rel_action_code($action_code_str) {
        $sql = 'select rel_action_code from ' . $this->table_name . ' where rel_action_code != '. "''" .' and action_code in (' . $action_code_str . ')';
        return $this->result_col($sql);
    }

    /**
     * 获取权限管理结构列表
     * @return boolean|array 结果 
     */
    public function get_list_struct(){
        $list = array();
        $data = $this->getAll();
        if($data) {
            //筛选出父级
            foreach ($data as $key => $row) {
                $parent_id = $row['parent_id'];
                if($parent_id == 0) {
                    $list[$row['action_id']] = $row;
                }
            }
            //组装子父关系，只限二级
            foreach ($list as $key => $row) {
                foreach ($data as $childrow) {
                    if($key == $childrow['parent_id']) {
                        $list[$key]['child'][] = $childrow;
                    }
                }
            }
            //重新排序
            foreach ($list as $key => $row) {
                $child = $row['child'];
                $sort_arr = array();
                if($child) {
                    foreach ($child as $_key => $_row) {
                        $sort_arr[$_key] = $_row['action_chinese'];
                    }                
                    array_multisort($sort_arr, SORT_DESC, $child);
                    $list[$key]['child'] = $child;
                }                
            }
            return $list;
        }
        return false;
    }
    /**
     * 獲取所有的數據
     * @return array 二維
     */
    public function get_list(){
        return $this->getAll();
    }
}
